<html>
<head><meta charset="utf-8"><title>Terminology &quot;structural match&quot; · t-lang · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/index.html">t-lang</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html">Terminology &quot;structural match&quot;</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="197384448"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/197384448" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#197384448">(May 13 2020 at 09:00)</a>:</h4>
<p>I keep being confused by the term "structural match". AFAIK it expresses a property of <code>PartialEq</code> and is very relevant not just to <code>match</code> but also to const generics (because they care about equality of values), so I am rather surprised that the name does not mention equality at all. Is there some deeper connection to matching that I am missing? is this term and its formal definition documented somewhere? (I checked the dev-guide and the reference, didn't find it.)<br>
Cc <span class="user-mention" data-user-id="124288">@oli</span> <span class="user-mention" data-user-id="116083">@pnkfelix</span> <span class="user-mention" data-user-id="216206">@lcnr</span></p>



<a name="197384520"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/197384520" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#197384520">(May 13 2020 at 09:00)</a>:</h4>
<p>(I am putting this in the t-lang stream because this seems to not just be a rustc implementation detail, but indeed part of the language spec)</p>



<a name="197384730"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/197384730" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#197384730">(May 13 2020 at 09:02)</a>:</h4>
<p>it's even weirder when someone talks about "implementing strcutural match", like <a href="https://github.com/rust-lang/rust/issues/72156">here</a>. that makes it sound like a trait, which maybe it is / should be? but then the text of the issue says "passes the structural match check" which does not sound like a trait at all.</p>



<a name="197384910"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/197384910" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#197384910">(May 13 2020 at 09:04)</a>:</h4>
<blockquote>
<p>it's even weirder when someone talks about "implementing strcutural match", like here.</p>
</blockquote>
<p>That's my bad <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span> I tend to think of this in terms of <code>StructuralEq</code> and <code>StructuralPartialEq</code>, which are actual traits.</p>



<a name="197385237"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/197385237" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#197385237">(May 13 2020 at 09:07)</a>:</h4>
<p>indeed there are traits, and even two of them</p>
<div class="codehilite"><pre><span></span><code>/// This implements the traversal over the structure of a given type to try to
/// find instances of ADTs (specifically structs or enums) that do not implement
/// the structural-match traits (`StructuralPartialEq` and `StructuralEq`).
</code></pre></div>



<a name="197385253"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/197385253" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#197385253">(May 13 2020 at 09:07)</a>:</h4>
<p>the traits are called "structural equality" though, not "structural match"</p>



<a name="197385312"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/197385312" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#197385312">(May 13 2020 at 09:08)</a>:</h4>
<p>and also, why does this not use the machinery that <code>Send</code>/<code>Sync</code>/<code>Freeze</code> etc use?</p>



<a name="197385465"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/197385465" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#197385465">(May 13 2020 at 09:09)</a>:</h4>
<p>Unfortunately <a href="https://doc.rust-lang.org/nightly/core/marker/trait.StructuralPartialEq.html">the docs</a> talk about when the trait is implemented but not about what the trait guarantees. and indeed it is a safe trait so it cannot actually guarantee anything?? now I am even more confused.^^</p>



<a name="197385955"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/197385955" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#197385955">(May 13 2020 at 09:14)</a>:</h4>
<p>My mental model of <code>StructuralPartialEq</code> trait is that it provides a guarantee about the <code>PartialEq</code> impl. But then (a) it should be <code>unsafe</code>, (b) it should "inherit" from <code>PartialEq</code>, and (c) <code>StructuralEq</code> is useless as I can't think of extra guarantees that it would introduce...</p>



<a name="197386032"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/197386032" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#197386032">(May 13 2020 at 09:15)</a>:</h4>
<p>what exactly that guarantee is (in terms of the <em>behavior</em> of <code>PartialEq::eq</code>) I am not sure and the docs don't say :/ I'll try to read through <a href="https://github.com/rust-lang/rust/issues/31434">the tracking issue and associated RFC</a> when I have some time</p>



<a name="197386497"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/197386497" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#197386497">(May 13 2020 at 09:19)</a>:</h4>
<p>or maybe since the trait is safe there actually isn't a guarantee and this is all basically just a lint?</p>



<a name="197387674"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/197387674" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#197387674">(May 13 2020 at 09:30)</a>:</h4>
<p>Ok, I looked into this a bit more and am still not completely sure my understanding is correct here.</p>
<p><code>StructuralEq</code> means "I am okay with comparing <code>self</code> using bitwise comparision". The reason that this is not unsafe is that while this may be surprising, it should not be able to cause any UB.</p>
<p>So both <code>StructuralEq</code> and <code>StructuralPartialEq</code> do not guarantee anything, they are more similar to <code>UnwindSafe</code> I guess <span aria-label="thinking" class="emoji emoji-1f914" role="img" title="thinking">:thinking:</span></p>



<a name="197388183"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/197388183" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#197388183">(May 13 2020 at 09:35)</a>:</h4>
<blockquote>
<p>and also, why does this not use the machinery that Send/Sync/Freeze etc use?</p>
</blockquote>
<p>These traits should be implemented for types implementing <code>PartialEq</code>/<code>Eq</code> only if they use <code>derive</code>. which means that we have to allow some manual (inside of <code>derive</code>) implementations. I currently don't know of a good way to check this inside of the compiler.</p>



<a name="197407560"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/197407560" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> varkor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#197407560">(May 13 2020 at 12:54)</a>:</h4>
<p><code>structural_match</code> was <a href="https://github.com/rust-lang/rfcs/blob/master/text/1445-restrict-constants-in-patterns.md">originally introduced</a> for pattern-matching on constants, hence "structural match". It has since been expanded to also cover equality for const generics, though in the future const generics may use more sophisticated equality testing.</p>



<a name="197407650"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/197407650" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> varkor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#197407650">(May 13 2020 at 12:55)</a>:</h4>
<p>It does not guarantee anything about how objects may be compared: just that they may be compared in certain contexts (pattern matching and const generics). In particular, it does not guarantee we can test for equality through bitwise equality.</p>



<a name="197407738"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/197407738" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> varkor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#197407738">(May 13 2020 at 12:56)</a>:</h4>
<p><code>structural_match</code> used to be an attribute, but is now implemented using two traits, <code>StructuralPartialEq</code> and <code>StructuralEq</code>: that's why there's some confusion, because the name changed without going via an RFC.</p>



<a name="197407834"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/197407834" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> varkor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#197407834">(May 13 2020 at 12:56)</a>:</h4>
<p>They are not <code>unsafe</code>, as I understand it, simply because they were never intended to be stabilised, though perhaps it would be pertinent to make them <code>unsafe</code> even for internal use.</p>



<a name="197407900"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/197407900" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> varkor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#197407900">(May 13 2020 at 12:57)</a>:</h4>
<p>They're both marker traits.</p>



<a name="197408010"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/197408010" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> varkor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#197408010">(May 13 2020 at 12:58)</a>:</h4>
<p>I don't think their current semantics is actually described in an RFC, as they have evolved organically: <span class="user-mention" data-user-id="116083">@pnkfelix</span> and <span class="user-mention" data-user-id="119009">@eddyb</span> are probably most up-to-date on the precise meaning, but I think even this is still subject to change.</p>



<a name="197408044"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/197408044" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> varkor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#197408044">(May 13 2020 at 12:58)</a>:</h4>
<p>They are auto-implemented for anything that derives <code>PartialEq</code> and <code>Eq</code>, because then the compiler has full control over equality, and so it should be safe to use in pattern matching or const generics.</p>



<a name="197414619"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/197414619" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#197414619">(May 13 2020 at 13:47)</a>:</h4>
<p>I don't think we would need to make the traits <code>unsafe</code>. The reason we have this restriction in place is not a safety concern (AFAIK), at least when it comes to its use in <em>match</em></p>



<a name="197414705"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/197414705" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#197414705">(May 13 2020 at 13:47)</a>:</h4>
<p>Its more that we didn't want to commit to a particular choice of semantics (which I think is explained in the RFC). Niko and I have spoken recently about wanting to untie this gordian knot</p>



<a name="197419068"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/197419068" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> varkor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#197419068">(May 13 2020 at 14:18)</a>:</h4>
<p>I think there could be safety concerns with const generics.</p>



<a name="197419196"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/197419196" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#197419196">(May 13 2020 at 14:19)</a>:</h4>
<p>yes, I do believe that may be true</p>



<a name="197419232"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/197419232" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#197419232">(May 13 2020 at 14:19)</a>:</h4>
<p>or at least ... well I'm not sure</p>



<a name="197419373"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/197419373" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#197419373">(May 13 2020 at 14:20)</a>:</h4>
<p>it certainly is <em>scary</em> to start having your type-equivalence relation depend upon user-defined code</p>



<a name="197419622"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/197419622" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#197419622">(May 13 2020 at 14:22)</a>:</h4>
<p>I would expect const generics to always use bitwise(structural) equivalence. So <code>StructuralEq</code> would mostly be an acknowledgement that this is the case.</p>



<a name="197419802"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/197419802" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#197419802">(May 13 2020 at 14:23)</a>:</h4>
<p>Which should prevent most safety concerns here.</p>



<a name="197419944"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/197419944" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#197419944">(May 13 2020 at 14:24)</a>:</h4>
<p>yes, but if bitwise equality simply <strong>is</strong> the right thing, then we could just use it directly</p>



<a name="197419986"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/197419986" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#197419986">(May 13 2020 at 14:24)</a>:</h4>
<p>which is an example of one way to untie the gordian knot</p>



<a name="197477973"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/197477973" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> varkor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#197477973">(May 13 2020 at 21:08)</a>:</h4>
<p>Bitwise equality is not the same as structural equality, particularly when it comes to unifying const expressions.</p>



<a name="197479536"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/197479536" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#197479536">(May 13 2020 at 21:22)</a>:</h4>
<p>hmm, looking at the following I don't yet get the difference here.</p>
<blockquote>
<p>Structural equality basically says "two things are structurally equal if their fields are structurally equal".</p>
</blockquote>
<p>and </p>
<blockquote>
<p>Primitive types like u32 are structurally equal if they represent the same value (but see below for discussion about floating point types like f32 and f64).<br>
That is, two values that are semantically unequal could be structurally equal (an example where this might occur is the floating point value NaN).</p>
</blockquote>
<p>Can you elaborate the differences between structural and bitwise equality?</p>



<a name="197489322"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/197489322" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> varkor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#197489322">(May 13 2020 at 23:17)</a>:</h4>
<p>I'll admit I was mainly thinking of expressions involving generic parameters, that we could unify without knowing their concrete values. Arguably this is still "structurally matching", albeit in a looser sense of the term, but is more sophisticated than checking directly for bitwise equality.</p>



<a name="197489454"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/197489454" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> varkor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#197489454">(May 13 2020 at 23:18)</a>:</h4>
<p>But I think <code>&amp;str</code> is structural match, but uses equality of strings rather than bitwise pointer equality.</p>



<a name="197492985"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/197492985" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rpjohnst <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#197492985">(May 14 2020 at 00:03)</a>:</h4>
<p><span class="user-mention silent" data-user-id="121053">varkor</span> <a href="#narrow/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22/near/197489454">said</a>:</p>
<blockquote>
<p>But I think <code>&amp;str</code> is structural match, but uses equality of strings rather than bitwise pointer equality.</p>
</blockquote>
<p>This case does involve soundness: <a href="https://github.com/rust-lang/const-eval/issues/42">https://github.com/rust-lang/const-eval/issues/42</a></p>



<a name="197519400"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/197519400" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#197519400">(May 14 2020 at 07:42)</a>:</h4>
<p>Does structural match need to be defined in the reference currently? If so, can somebody give me a definition?</p>



<a name="197537500"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/197537500" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> varkor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#197537500">(May 14 2020 at 11:02)</a>:</h4>
<p>It's not stable and is still undergoing design discussions, so probably better to wait.</p>



<a name="197595217"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/197595217" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#197595217">(May 14 2020 at 18:08)</a>:</h4>
<p><span class="user-mention silent" data-user-id="216206">lcnr</span> <a href="#narrow/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22/near/197387674">said</a>:</p>
<blockquote>
<p><code>StructuralEq</code> means "I am okay with comparing <code>self</code> using bitwise comparision". The reason that this is not unsafe is that while this may be surprising, it should not be able to cause any UB.</p>
</blockquote>
<p>that can't be true. the following type is not okay with bitwise comparison due to padding: <code>(u8, u16)</code>.</p>
<blockquote>
<p>They are not unsafe, as I understand it, simply because they were never intended to be stabilised, though perhaps it would be pertinent to make them unsafe even for internal use.</p>
</blockquote>
<p>Safety and stability are orthogonal concerns.. or IOW, if libstd can cause UB without unsafe code that's still a soundess bug.<br>
so if there are any semantic guarantees attached to this trait, it should be made unsafe IMO.</p>



<a name="197595269"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/197595269" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#197595269">(May 14 2020 at 18:09)</a>:</h4>
<p>so bitwise equality is <em>not</em> what we want. at best we want "bitwise padding-aware equality".</p>



<a name="197595429"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/197595429" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#197595429">(May 14 2020 at 18:10)</a>:</h4>
<p><span class="user-mention silent" data-user-id="121053">varkor</span> <a href="#narrow/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22/near/197489454">said</a>:</p>
<blockquote>
<p>But I think <code>&amp;str</code> is structural match, but uses equality of strings rather than bitwise pointer equality.</p>
</blockquote>
<p>("bitwise pointer equality" was not what I meant by "bitwise equality", FWIW)</p>



<a name="197595442"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/197595442" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#197595442">(May 14 2020 at 18:10)</a>:</h4>
<p>(also see <a href="https://github.com/rust-lang/rust/issues/70889">https://github.com/rust-lang/rust/issues/70889</a> )</p>



<a name="197595537"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/197595537" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#197595537">(May 14 2020 at 18:11)</a>:</h4>
<p>for const generics we probably want "it's the same as equality of @eddyb's 'value tree'"</p>



<a name="197595598"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/197595598" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#197595598">(May 14 2020 at 18:11)</a>:</h4>
<p>which indeed is a property that is <em>preserved</em> by <code>derive(PartialEq)</code> so the setup makes sense for that -- except that <code>Eq</code> is entirely irrelevant so there would be no soundness reason to have <code>StructuralEq</code></p>



<a name="197598233"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/197598233" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthew Jasper <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#197598233">(May 14 2020 at 18:30)</a>:</h4>
<p><code>Eq</code> is relevant for "does not contain floats"</p>



<a name="197785294"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/197785294" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#197785294">(May 16 2020 at 08:57)</a>:</h4>
<p><span class="user-mention" data-user-id="116118">@Matthew Jasper</span>  floats just shouldn't implement <code>StructuralPartialEq</code> if their equality test doesnt satisfy whatever guarantees we need?</p>



<a name="201582843"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/201582843" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#201582843">(Jun 22 2020 at 09:16)</a>:</h4>
<p>Cc <a href="https://github.com/rust-lang/rust/issues/73448">https://github.com/rust-lang/rust/issues/73448</a> (mostly leaving a note for myself)</p>



<a name="204229519"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/204229519" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#204229519">(Jul 17 2020 at 16:41)</a>:</h4>
<p>I wrote something up: <a href="https://github.com/rust-lang/rust/issues/74446">https://github.com/rust-lang/rust/issues/74446</a></p>



<a name="204229671"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/204229671" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#204229671">(Jul 17 2020 at 16:43)</a>:</h4>
<p>feedback welcome :)<br>
(but I'm in the train towards a 1-week vacation; I'll see what happens when I come back. :D )</p>



<a name="204787855"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Terminology%20%22structural%20match%22/near/204787855" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Terminology.20.22structural.20match.22.html#204787855">(Jul 23 2020 at 12:09)</a>:</h4>
<p>(more like <span aria-label="mountain" class="emoji emoji-26f0" role="img" title="mountain">:mountain:</span> for me but thanks :D )</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>